package net.tv90.bfmccore.game.impl.cq;

import net.tv90.bfmccore.game.GameManager;
import net.tv90.bfmccore.game.GameUtils;
import net.tv90.bfmccore.game.view.GameModelViewHandler;
import net.tv90.bfmccore.game.impl.EnumSpawnPolicy;
import net.tv90.bfmccore.game.impl.vehicle.IVehicleMenuGenerator;
import net.tv90.bfmccore.game.impl.vehicle.VehicleGroup;
import net.tv90.bfmccore.helper.FactionHelper;
import net.tv90.bfmccore.util.area.Area;
import net.tv90.bfmccore.util.vector.Vector3i;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;

import java.util.ArrayList;
import java.util.List;

import static net.tv90.bfmccore.helper.FactionHelper.getCountry2;

@VehicleSpawn
public class VehicleSpawnConquestArea extends ConquestArea implements IVehicleSpawnConquestArea, IVehicleMenuGenerator {


    public boolean hasLight = false;
    public boolean hasTank = false;
    public boolean hasAA = false;
    public boolean hasHeli = false;
    public boolean hasTransHeli = false;
    public boolean hasJet = false;
    public boolean hasAtkJet = false;
    public boolean hasFac = false;

    public List<Vector3i> firstLightSpawnPoint;
    public List<Vector3i> firstTankSpawnPoint;
    public List<Vector3i> firstAASpawnPoint;
    public List<Vector3i> firstHeliSpawnPoint;
    public List<Vector3i> firstTransHeliSpawnPoint;
    public List<Vector3i> firstJetSpawnPoint;
    public List<Vector3i> firstAtkJetSpawnPoint;
    public List<Vector3i> firstFacSpawnPoint;

    public List<Vector3i> secondLightSpawnPoint;
    public List<Vector3i> secondTankSpawnPoint;
    public List<Vector3i> secondAASpawnPoint;
    public List<Vector3i> secondHeliSpawnPoint;
    public List<Vector3i> secondTransHeliSpawnPoint;
    public List<Vector3i> secondJetSpawnPoint;
    public List<Vector3i> secondAtkJetSpawnPoint;
    public List<Vector3i> secondFacSpawnPoint;

    public List<String> firstLightVehicleGroup;
    public List<String> firstTankVehicleGroup;
    public List<String> firstAAVehicleGroup;
    public List<String> firstHeliVehicleGroup;
    public List<String> firstTransHeliVehicleGroup;
    public List<String> firstJetVehicleGroup;
    public List<String> firstAtkJetVehicleGroup;
    public List<String> firstFacVehicleGroup;

    public List<String> secondLightVehicleGroup;
    public List<String> secondTankVehicleGroup;
    public List<String> secondAAVehicleGroup;
    public List<String> secondHeliVehicleGroup;
    public List<String> secondTransHeliVehicleGroup;
    public List<String> secondJetVehicleGroup;
    public List<String> secondAtkJetVehicleGroup;
    public List<String> secondFacVehicleGroup;

    public VehicleSpawnConquestArea(Area area, String index, String name, boolean canCapture, List<Vector3i> firstFactionSpawn, List<Vector3i> firstFactionSpawnWhenCapturing, List<Vector3i> secondFactionSpawn, List<Vector3i> secondFactionSpawnWhenCapturing) {
        super(area, index, name, canCapture, firstFactionSpawn, firstFactionSpawnWhenCapturing, secondFactionSpawn, secondFactionSpawnWhenCapturing);
    }

    @Override
    public List<VehicleGroup> getFirstFactionVehicleGroup() {
        return getVehicleGroups(firstLightVehicleGroup, firstTankVehicleGroup, firstAAVehicleGroup, firstHeliVehicleGroup, firstTransHeliVehicleGroup, firstJetVehicleGroup, firstAtkJetVehicleGroup, firstFacVehicleGroup);
    }

    @Override
    public List<VehicleGroup> getSecondFactionVehicleGroup() {
        return getVehicleGroups(secondLightVehicleGroup, secondTankVehicleGroup, secondAAVehicleGroup, secondHeliVehicleGroup, secondTransHeliVehicleGroup, secondJetVehicleGroup, secondAtkJetVehicleGroup, secondFacVehicleGroup);
    }

    private List<VehicleGroup> getVehicleGroups(List<String> lightVehicleGroup, List<String> tankVehicleGroup, List<String> aaVehicleGroup, List<String> heliVehicleGroup, List<String> transHeliVehicleGroup, List<String> jetVehicleGroup, List<String> atkJetVehicleGroup, List<String> facVehicleGroup) {
        List<VehicleGroup> groups = new ArrayList<>();
        if (lightVehicleGroup != null && !lightVehicleGroup.isEmpty()) {
            groups.add(new VehicleGroup("LIGHT", index, lightVehicleGroup, false));
        }
        if (tankVehicleGroup != null && !tankVehicleGroup.isEmpty()) {
            groups.add(new VehicleGroup("TANK", index, tankVehicleGroup, false));
        }
        if (aaVehicleGroup != null && !aaVehicleGroup.isEmpty()) {
            groups.add(new VehicleGroup("AA", index, aaVehicleGroup, false));
        }
        if (heliVehicleGroup != null && !heliVehicleGroup.isEmpty()) {
            groups.add(new VehicleGroup("HELI", index, heliVehicleGroup, false));
        }
        if (transHeliVehicleGroup != null && !transHeliVehicleGroup.isEmpty()) {
            groups.add(new VehicleGroup("TRANS", index, transHeliVehicleGroup, false));
        }
        if (jetVehicleGroup != null && !jetVehicleGroup.isEmpty()) {
            groups.add(new VehicleGroup("JET", index, jetVehicleGroup, false));
        }
        if (atkJetVehicleGroup != null && !atkJetVehicleGroup.isEmpty()) {
            groups.add(new VehicleGroup("ATKJET", index, atkJetVehicleGroup, false));
        }
        if (facVehicleGroup != null && !facVehicleGroup.isEmpty()) {
            groups.add(new VehicleGroup("FAC", index, facVehicleGroup, false));
        }
        return groups;
    }


    @Override
    public void teleportPlayerToConquestArea(Player p) {
        if (checkIfCanDeploy(p)) {
            openVehicleMenu(p);
        }
    }

    public void openVehicleMenu(Player p) {
        String country = FactionHelper.getCountry2(p);
        if ("NoFaction".equals(country)) {
            p.sendMessage("§e[管理员]你还没有选择阵营");
            return;
        }
        GameModelViewHandler.getInstance().createVehicleDispatcherGui(p, this);
    }

    @Override
    public void teleportPlayerToVehicleConquestArea(Player p, String type, String vehicle) {
        if (canSelectVehicle(p, type)) {
            String firstFactionName = GameManager.getInstance().currentGame.firstFactionName;
            String secondFactionName = GameManager.getInstance().currentGame.secondFactionName;
            String country = getCountry2(p);
            Vector3i vec = null;
            if (country.equals(firstFactionName)) {
                switch (type) {
                    case "AA": {
                        vec = GameUtils.getExactSpawnPoint(p, firstAASpawnPoint, EnumSpawnPolicy.RANDOM);
                        break;
                    }
                    case "LIGHT": {
                        vec = GameUtils.getExactSpawnPoint(p, firstLightSpawnPoint, EnumSpawnPolicy.RANDOM);
                        break;
                    }
                    case "TANK": {
                        vec = GameUtils.getExactSpawnPoint(p, firstTankSpawnPoint, EnumSpawnPolicy.RANDOM);
                        break;
                    }
                    case "JET": {
                        vec = GameUtils.getExactSpawnPoint(p, firstJetSpawnPoint, EnumSpawnPolicy.RANDOM);
                        break;
                    }
                    case "ATKJET": {
                        vec = GameUtils.getExactSpawnPoint(p, firstAtkJetSpawnPoint, EnumSpawnPolicy.RANDOM);
                        break;
                    }
                    case "FAC": {
                        vec = GameUtils.getExactSpawnPoint(p, firstFacSpawnPoint, EnumSpawnPolicy.RANDOM);
                        break;
                    }
                    case "HELI": {
                        vec = GameUtils.getExactSpawnPoint(p, firstHeliSpawnPoint, EnumSpawnPolicy.RANDOM);
                        break;
                    }
                    case "TRANS": {
                        vec = GameUtils.getExactSpawnPoint(p, firstTransHeliSpawnPoint, EnumSpawnPolicy.RANDOM);
                        break;
                    }
                    case "INF": {
                        vec = GameUtils.getExactSpawnPoint(p, firstFactionSpawn, EnumSpawnPolicy.CLOSEST_TO_TEAMMATE);
                        break;
                    }
                }
            } else if (country.equals(secondFactionName)) {
                switch (type) {
                    case "AA": {
                        vec = GameUtils.getExactSpawnPoint(p, secondAASpawnPoint, EnumSpawnPolicy.RANDOM);
                        break;
                    }
                    case "LIGHT": {
                        vec = GameUtils.getExactSpawnPoint(p, secondLightSpawnPoint, EnumSpawnPolicy.RANDOM);
                        break;
                    }
                    case "TANK": {
                        vec = GameUtils.getExactSpawnPoint(p, secondTankSpawnPoint, EnumSpawnPolicy.RANDOM);
                        break;
                    }
                    case "JET": {
                        vec = GameUtils.getExactSpawnPoint(p, secondJetSpawnPoint, EnumSpawnPolicy.RANDOM);
                        break;
                    }
                    case "ATKJET": {
                        vec = GameUtils.getExactSpawnPoint(p, secondAtkJetSpawnPoint, EnumSpawnPolicy.RANDOM);
                        break;
                    }
                    case "FAC": {
                        vec = GameUtils.getExactSpawnPoint(p, secondFacSpawnPoint, EnumSpawnPolicy.RANDOM);
                        break;
                    }
                    case "HELI": {
                        vec = GameUtils.getExactSpawnPoint(p, secondHeliSpawnPoint, EnumSpawnPolicy.RANDOM);
                        break;
                    }
                    case "TRANS": {
                        vec = GameUtils.getExactSpawnPoint(p, secondTransHeliSpawnPoint, EnumSpawnPolicy.RANDOM);
                        break;
                    }
                    case "INF": {
                        vec = GameUtils.getExactSpawnPoint(p, secondFactionSpawn, EnumSpawnPolicy.CLOSEST_TO_TEAMMATE);
                        break;
                    }
                }
            } else {
                GameManager.getInstance().currentGame.teleportSinglePlayer2(p, GameManager.getInstance().currentGame.mainSpawnPoint);
            }
            if (vec != null) {
                GameManager.getInstance().currentGame.teleportSinglePlayer2(p, vec);
                GameUtils.vehicleDeploy(p);
                Bukkit.dispatchCommand(Bukkit.getConsoleSender(), "give " + p.getName() + " mcheli:" + vehicle + " 1 0");
            }
        }
    }

    public VehicleSpawnConquestArea setHasTank() {
        this.hasTank = true;
        return this;
    }

    public VehicleSpawnConquestArea setHasAA() {
        this.hasAA = true;
        return this;
    }

    public VehicleSpawnConquestArea setHasHeli() {
        this.hasHeli = true;
        return this;
    }

    public VehicleSpawnConquestArea setHasTransHeli() {
        this.hasTransHeli = true;
        return this;
    }

    public VehicleSpawnConquestArea setHasJet() {
        this.hasJet = true;
        return this;
    }

    public VehicleSpawnConquestArea setHasAtkJet() {
        this.hasAtkJet = true;
        return this;
    }

    public VehicleSpawnConquestArea setHasFac() {
        this.hasFac = true;
        return this;
    }

    public VehicleSpawnConquestArea setHasLight() {
        this.hasLight = true;
        return this;
    }

    @Override
    public String getIndex() {
        return index;
    }

    @Override
    public boolean hasLight() {
        return hasLight;
    }

    @Override
    public boolean hasTank() {
        return hasTank;
    }

    @Override
    public boolean hasAA() {
        return hasAA;
    }

    @Override
    public boolean hasHeli() {
        return hasHeli;
    }

    @Override
    public boolean hasTransHeli() {
        return hasTransHeli;
    }

    @Override
    public boolean hasJet() {
        return hasJet;
    }

    @Override
    public boolean hasAtkJet() {
        return hasAtkJet;
    }

    @Override
    public boolean hasFac() {
        return hasFac;
    }
    @Override
    public VehicleSpawnConquestArea setFirstLightSpawnPoint(List<Vector3i> firstLightSpawnPoint) {
        this.firstLightSpawnPoint = firstLightSpawnPoint;
        return this;
    }
    @Override
    public VehicleSpawnConquestArea setFirstTankSpawnPoint(List<Vector3i> firstTankSpawnPoint) {
        this.firstTankSpawnPoint = firstTankSpawnPoint;
        return this;
    }
    @Override
    public VehicleSpawnConquestArea setFirstAASpawnPoint(List<Vector3i> firstAASpawnPoint) {
        this.firstAASpawnPoint = firstAASpawnPoint;
        return this;
    }
    @Override
    public VehicleSpawnConquestArea setFirstHeliSpawnPoint(List<Vector3i> firstHeliSpawnPoint) {
        this.firstHeliSpawnPoint = firstHeliSpawnPoint;
        return this;
    }
    @Override
    public VehicleSpawnConquestArea setFirstTransHeliSpawnPoint(List<Vector3i> firstTransHeliSpawnPoint) {
        this.firstTransHeliSpawnPoint = firstTransHeliSpawnPoint;
        return this;
    }
    @Override
    public VehicleSpawnConquestArea setFirstJetSpawnPoint(List<Vector3i> firstJetSpawnPoint) {
        this.firstJetSpawnPoint = firstJetSpawnPoint;
        return this;
    }
    @Override
    public VehicleSpawnConquestArea setFirstAtkJetSpawnPoint(List<Vector3i> firstAtkJetSpawnPoint) {
        this.firstAtkJetSpawnPoint = firstAtkJetSpawnPoint;
        return this;
    }
    @Override
    public VehicleSpawnConquestArea setFirstFacSpawnPoint(List<Vector3i> firstFacSpawnPoint) {
        this.firstFacSpawnPoint = firstFacSpawnPoint;
        return this;
    }
    @Override
    public VehicleSpawnConquestArea setSecondLightSpawnPoint(List<Vector3i> secondLightSpawnPoint) {
        this.secondLightSpawnPoint = secondLightSpawnPoint;
        return this;
    }
    @Override
    public VehicleSpawnConquestArea setSecondTankSpawnPoint(List<Vector3i> secondTankSpawnPoint) {
        this.secondTankSpawnPoint = secondTankSpawnPoint;
        return this;
    }
    @Override
    public VehicleSpawnConquestArea setSecondAASpawnPoint(List<Vector3i> secondAASpawnPoint) {
        this.secondAASpawnPoint = secondAASpawnPoint;
        return this;
    }
    @Override
    public VehicleSpawnConquestArea setSecondHeliSpawnPoint(List<Vector3i> secondHeliSpawnPoint) {
        this.secondHeliSpawnPoint = secondHeliSpawnPoint;
        return this;
    }
    @Override
    public VehicleSpawnConquestArea setSecondTransHeliSpawnPoint(List<Vector3i> secondTransHeliSpawnPoint) {
        this.secondTransHeliSpawnPoint = secondTransHeliSpawnPoint;
        return this;
    }
    @Override
    public VehicleSpawnConquestArea setSecondJetSpawnPoint(List<Vector3i> secondJetSpawnPoint) {
        this.secondJetSpawnPoint = secondJetSpawnPoint;
        return this;
    }
    @Override
    public VehicleSpawnConquestArea setSecondAtkJetSpawnPoint(List<Vector3i> secondAtkJetSpawnPoint) {
        this.secondAtkJetSpawnPoint = secondAtkJetSpawnPoint;
        return this;
    }
    @Override
    public VehicleSpawnConquestArea setSecondFacSpawnPoint(List<Vector3i> secondFacSpawnPoint) {
        this.secondFacSpawnPoint = secondFacSpawnPoint;
        return this;
    }
    @Override
    public VehicleSpawnConquestArea setFirstLightVehicleGroup(List<String> firstLightVehicleGroup) {
        this.firstLightVehicleGroup = firstLightVehicleGroup;
        return this;
    }
    @Override
    public VehicleSpawnConquestArea setFirstTankVehicleGroup(List<String> firstTankVehicleGroup) {
        this.firstTankVehicleGroup = firstTankVehicleGroup;
        return this;
    }
    @Override
    public VehicleSpawnConquestArea setFirstAAVehicleGroup(List<String> firstAAVehicleGroup) {
        this.firstAAVehicleGroup = firstAAVehicleGroup;
        return this;
    }
    @Override
    public VehicleSpawnConquestArea setFirstHeliVehicleGroup(List<String> firstHeliVehicleGroup) {
        this.firstHeliVehicleGroup = firstHeliVehicleGroup;
        return this;
    }
    @Override
    public VehicleSpawnConquestArea setFirstTransHeliVehicleGroup(List<String> firstTransHeliVehicleGroup) {
        this.firstTransHeliVehicleGroup = firstTransHeliVehicleGroup;
        return this;
    }
    @Override
    public VehicleSpawnConquestArea setFirstJetVehicleGroup(List<String> firstJetVehicleGroup) {
        this.firstJetVehicleGroup = firstJetVehicleGroup;
        return this;
    }
    @Override
    public VehicleSpawnConquestArea setFirstAtkJetVehicleGroup(List<String> firstAtkJetVehicleGroup) {
        this.firstAtkJetVehicleGroup = firstAtkJetVehicleGroup;
        return this;
    }
    @Override
    public VehicleSpawnConquestArea setFirstFacVehicleGroup(List<String> firstFacVehicleGroup) {
        this.firstFacVehicleGroup = firstFacVehicleGroup;
        return this;
    }
    @Override
    public VehicleSpawnConquestArea setSecondLightVehicleGroup(List<String> secondLightVehicleGroup) {
        this.secondLightVehicleGroup = secondLightVehicleGroup;
        return this;
    }
    @Override
    public VehicleSpawnConquestArea setSecondTankVehicleGroup(List<String> secondTankVehicleGroup) {
        this.secondTankVehicleGroup = secondTankVehicleGroup;
        return this;
    }
    @Override
    public VehicleSpawnConquestArea setSecondAAVehicleGroup(List<String> secondAAVehicleGroup) {
        this.secondAAVehicleGroup = secondAAVehicleGroup;
        return this;
    }
    @Override
    public VehicleSpawnConquestArea setSecondHeliVehicleGroup(List<String> secondHeliVehicleGroup) {
        this.secondHeliVehicleGroup = secondHeliVehicleGroup;
        return this;
    }
    @Override
    public VehicleSpawnConquestArea setSecondTransHeliVehicleGroup(List<String> secondTransHeliVehicleGroup) {
        this.secondTransHeliVehicleGroup = secondTransHeliVehicleGroup;
        return this;
    }
    @Override
    public VehicleSpawnConquestArea setSecondJetVehicleGroup(List<String> secondJetVehicleGroup) {
        this.secondJetVehicleGroup = secondJetVehicleGroup;
        return this;
    }
    @Override
    public VehicleSpawnConquestArea setSecondAtkJetVehicleGroup(List<String> secondAtkJetVehicleGroup) {
        this.secondAtkJetVehicleGroup = secondAtkJetVehicleGroup;
        return this;
    }
    @Override
    public VehicleSpawnConquestArea setSecondFacVehicleGroup(List<String> secondFacVehicleGroup) {
        this.secondFacVehicleGroup = secondFacVehicleGroup;
        return this;
    }
}
